
package com.bj58.disconf.web.controllers;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.baidu.disconf.core.common.constants.DisConfigTypeEnum;
import com.bj58.disconf.web.PrintBeat;
import com.bj58.disconf.web.common.vo.JsonObjectBase;
import com.bj58.disconf.web.controllers.config.validator.ConfigValidator;
import com.bj58.disconf.web.controllers.config.validator.FileUploadValidator;
import com.bj58.disconf.web.service.config.form.ConfNewForm;
import com.bj58.disconf.web.service.config.form.ConfNewItemForm;
import com.bj58.disconf.web.service.config.service.ConfigMgr;
import com.bj58.disconf.web.utils.GuiceDI;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.annotation.Path;

/**
 * 专用于配置新建
 *
 * @author lemon
 * @version 2014-6-24
 */
@Path("/api/web/config")
public class ConfigNewController extends BaseController {

    protected static final Logger LOG = LoggerFactory.getLogger(ConfigUpdateController.class);

    private final ConfigMgr configMgr = GuiceDI.getInstance(ConfigMgr.class);

    private final ConfigValidator configValidator = GuiceDI.getInstance(ConfigValidator.class);

    private final FileUploadValidator fileUploadValidator = GuiceDI.getInstance(FileUploadValidator.class);

    /**
     * 配置项的新建
     *
     * @param confNewForm
     *
     * @return
     */
    @Path("/item")
    public ActionResult newItem(@Valid ConfNewItemForm confNewForm) {

        try {
            PrintBeat.printBeat(beat);
            // 业务校验
            configValidator.validateNew(confNewForm, DisConfigTypeEnum.ITEM);

            //
            configMgr.newConfig(confNewForm, DisConfigTypeEnum.ITEM);

            return new JsonActionResult(buildSuccess("创建成功"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 配置文件的新建(使用上传配置文件)
     *
     * @param confNewForm
     * @param file
     *
     * @return
     */
    @Path("/file")
    public ActionResult updateFile(@Valid ConfNewForm confNewForm, @RequestParam("myfilerar") MultipartFile file) {

        PrintBeat.printBeat(beat);
        //
        // 校验
        //
        int fileSize = 1024 * 1024 * 4;
        String[] allowExtName = { ".properties", ".xml" };
        fileUploadValidator.validateFile(file, fileSize, allowExtName);

        //
        // 更新
        //
        String fileContent = "";
        try {

            fileContent = new String(file.getBytes(), "UTF-8");
            LOG.info("receive file: " + fileContent);

            // 创建配置文件表格
            ConfNewItemForm confNewItemForm = new ConfNewItemForm(confNewForm);
            confNewItemForm.setKey(file.getOriginalFilename());
            confNewItemForm.setValue(fileContent);

            // 业务校验
            configValidator.validateNew(confNewItemForm, DisConfigTypeEnum.FILE);

            //
            configMgr.newConfig(confNewItemForm, DisConfigTypeEnum.FILE);

            return new JsonActionResult(buildSuccess("创建成功"));
        } catch (Exception e) {
            LOG.error(e.toString());
        }
        return null;
    }

    /**
     * 配置文件的新建(使用文本)
     *
     * @param confNewForm
     * @param fileContent
     * @param fileName
     *
     * @return
     */
    @Path("/filetext")
    public ActionResult updateFileWithText(@Valid ConfNewForm confNewForm, @NotNull String fileContent, @NotNull String fileName) {

        try {
            PrintBeat.printBeat(beat);

            // 创建配置文件表格
            ConfNewItemForm confNewItemForm = new ConfNewItemForm(confNewForm);
            confNewItemForm.setKey(fileName);
            confNewItemForm.setValue(fileContent);

            // 业务校验
            configValidator.validateNew(confNewItemForm, DisConfigTypeEnum.FILE);

            //
            configMgr.newConfig(confNewItemForm, DisConfigTypeEnum.FILE);

            return new JsonActionResult(buildSuccess("创建成功"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
